package com.apes.fn.scm.commodityInquiry.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.OutboxPayloadWrapper;
import com.apes.framework.plugin.outbox.OutboxSubscriberService;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.pi.service.PiManager;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.repository.AreaRepository;
import com.apes.scm.masterdata.frame.model.Store;
import com.apes.scm.masterdata.frame.repository.StoreAreaInquiryRepository;
import com.apes.scm.masterdata.frame.repository.StoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lishikun on 2020/12/11 16:42
 */
@Service("storeAreaInquiryService")
public class StoreAreaInquiryService extends DomainService {

    @Autowired
    private StoreAreaInquiryRepository storeAreaInquiryRepository;

    @Autowired
    private PiManager piManager;

    @Autowired
    private OutboxSubscriberService outboxSubscriberService;

    @Autowired
    private AreaRepository areaRepository;

    @Autowired
    private StoreRepository storeRepository;

    public Store update(SimpleRequest request){
        Store store = request.getO(Store.class);
        resolveStoreAreaInquiryItem(store);// 处理省、城市
        store = storeRepository.saveAndFlush(store);

        this.synOldStoreAreaInquiry(store);// 同步到老框架
        return store;
    }

    /**
     * 把数据同步到老框架
     * @param store
     */
    private void synOldStoreAreaInquiry(Store store) {
        JSONArray singleStoreAreaInquiryArray = new JSONArray();
        store.getStoreAreaInquiries().forEach(storeAreaInquiry -> {
            JSONObject storeAreaInquiryObj = new JSONObject();
            storeAreaInquiryObj.put("VALID", storeAreaInquiry.isValid() == true?1:0);
            storeAreaInquiryObj.put("CITY_ID", storeAreaInquiry.getCity().getId());
            storeAreaInquiryObj.put("PROVINCE_ID", storeAreaInquiry.getProvince().getId());
            storeAreaInquiryObj.put("STORE_ID", storeAreaInquiry.getStore().getId());
            storeAreaInquiryObj.put("ID", storeAreaInquiry.getId());
            storeAreaInquiryObj.put("STORE_ORIGIN", storeAreaInquiry.getStoreOrigin());
            singleStoreAreaInquiryArray.add(storeAreaInquiryObj);
        });
        JSONObject returnData = new JSONObject();
        returnData.put("STORE_ID", store.getId());
        returnData.put("data", singleStoreAreaInquiryArray);
        outboxSubscriberService.commonSendRequest("local:storeAreaInquiryService.synStoreAreaInquiery", store.getId(), returnData);
    }

    @OutboxSubscriber(id="local:storeAreaInquiryService.synStoreAreaInquiery", name="门店区域询价同步到老框架")
    private void synStoreAreaInquiery(OutboxPayloadWrapper outboxPayloadWrapper) {
        piManager.synErp("mdmStoreAreaInquiryService.update", outboxPayloadWrapper.getPayload());
    }

    /***
     * 处理省/市地址
     */
    private void resolveStoreAreaInquiryItem(Store store) {
        Map<Area,Area> map = new HashMap<>();
        store.getStoreAreaInquiries().stream().forEach(item -> {
            if( item.getCity() == null ) return;
            Area regCity = item.getCity();// 当前城市
            String regCityId = regCity.getParent(); //当前城市的省市编码
            Area regProvince = areaRepository.findOne(regCityId); //当前省份
            for (Map.Entry<Area,Area> entry : map.entrySet()){
                if (entry.getKey() == regProvince && entry.getValue() == regCity){
                    map.clear();
                    throw new RuntimeException( regProvince.getName() + regCity.getName() + "已存在相关信息");
                }
            }
            map.put(regProvince,regCity);
            item.setCity(regCity); // 注册地址城市
            item.setProvince(regProvince); //注册地址.省份
            item.setStoreOrigin(store.getOrigin());
        });
    }

}
